package com.share.test.example.groovy

import com.share.common.utils.DelayUtil
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

public Object parse(WebDriver driver) {
    //打开创建促销页面---需要加载页面
    String url = "https://sellercentral.amazon.com/promotions/manage/ref=xx_promomgr_favb_xx";
    driver.get(url);
    DelayUtil.sleepRangSecond(2, 5);

    //点击促销---需要加载页面
    WebElement ele_cj = driver.findElement(By.id("epmMPCLink"));
    ele_cj.click();
    DelayUtil.sleepRangSecond(3, 4);

    //商品下拉列表-----完整的提示
    println("商品下拉列表")
    WebElement ele_goods = driver.findElement(By.id("includeId"));
    ele_goods.click();
    DelayUtil.sleepRangSecond(1, 3);
    WebElement ele_goods1 = driver.findElement(By.id("includeIdNative_1"))
    String tx = ele_goods1.text; //LED Strip Lights with Bluetooth and APP Control,Multicolor RGB LED Light Strips
    ele_goods1.click();
    println("--------------------------------" + tx)

    //填写折扣率
    WebElement ele_55 = driver.findElement(By.id("benefitValue"));
    ele_55.clear();//先请空再填写
    ele_55.sendKeys("15");

    //促销开始日期
    WebElement ele_promotion_start_date = driver.findElement(By.id("startDate"));
    ele_promotion_start_date.clear()
    ele_promotion_start_date.sendKeys("2023-10-12")

    //选择时间
    WebElement ele_promotion_start_time = driver.findElement(By.id("startTime"));
    ele_promotion_start_time.click()
    DelayUtil.sleepRangSecond(1, 3);
    WebElement start_time = driver.findElement(By.id("startTimeNative_18"));
    start_time.click()

    //促销结束日期
    WebElement ele_promotion_end_date = driver.findElement(By.id("endDate"));
    ele_promotion_end_date.clear()
    ele_promotion_end_date.sendKeys("2023-10-18")

    //结束时间
    WebElement ele_promotion_end_time = driver.findElement(By.id("endTime"));
    ele_promotion_end_time.click()
    DelayUtil.sleepRangSecond(1, 3);
    WebElement endTime = driver.findElement(By.id("endTimeNative_9"));
    endTime.click()

    //促销的产品
    WebElement ele_promotion_item = driver.findElement(By.id("promotionTitle"));
    ele_promotion_item.sendKeys("Led Lights for Bedroom 65.6ft, LED Strip Lights with Bluetooth---2");

    //无限结算
    WebElement ele_wuxianjisuan = driver.findElement(By.id("unlimitedProductUnlimitedOrderRadio"));
    ele_wuxianjisuan.click();

    //点击查看----初步提交
    WebElement ele_view = driver.findElement(By.id("a-autoid-3-announce"));
    ele_view.click();
    DelayUtil.sleepRangSecond(3, 5);
    String curReviewUrl = driver.getCurrentUrl();
    if (curReviewUrl.startsWith("https://sellercentral.amazon.com/promotions/new/preview")) {
        println(driver.getPageSource());//打印订单源码
        println("打开预览页面成功");
    } else {
        println("初步预览失败");
        return 1;
    }

    // 点击提交
    WebElement ele_commit = driver.findElement(By.id("a-autoid-1-announce"));
    ele_commit.click();
    DelayUtil.sleepRangSecond(3, 5);
    if (curReviewUrl.startsWith("https://sellercentral.amazon.com/promotions/new/submit#&tab=create-your-promotions")) {
        println(driver.getPageSource());//打印订单源码
        println("打开提交页面成功");
    } else {
        println("提交失败");
        return 1;
    }

    // 获取促销链接
    WebElement ele_link = driver.findElement(By.id("//*[@id=\"epmPromotionSuccessMessage\"]/div/div/p[1]/text()[1]"));
    String link = ele_link.text;
    println("促销链接=" + link)
}
